package cn.cena.user.controller;


import cn.cena.basic.util.AjaxResult;
import cn.cena.basic.util.PageList;
import cn.cena.user.domain.Logininfo;
import cn.cena.user.query.LogininfoQuery;
import cn.cena.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
@Api(tags = "登录接口",description = "登录接口详细描述")
public class LogininfoController {
    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 查询一条
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation(value = "查询一条数据",notes = "传入一个id")
    public Logininfo loadOne(@PathVariable("id")Long id){
        return logininfoService.loadOne(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "不需要参数")
    public List<Logininfo> loadAll(){
        return logininfoService.loadAll();
    }

    /**
     *  高级查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "分页")
    public PageList<Logininfo> loadByQuery(@RequestBody LogininfoQuery query){
        return logininfoService.queryData(query);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id删除",notes = "传入一个id")
    public AjaxResult del(@PathVariable Long id){
        try {
            logininfoService.remove(id);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("删除失败");
        }
    }
    @PatchMapping
    @ApiOperation(value = "根据id批量删除",notes = "传入多个id")
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            logininfoService.patchRemove(ids);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "登录添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addAndUpdate(@RequestBody Logininfo logininfo){
        try {
            if (logininfo.getId()==null){
                logininfoService.add(logininfo);
            } else {
                logininfoService.update(logininfo);
            }
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("操作失败");
        }
    }
}
